package com.heyun.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunSalesPlan;
import com.heyun.bean.Message;
import com.heyun.service.impl.SalesPlanServiceImpl;

//销售计划展示
@Controller
@RequestMapping("/SalesPlan")
public class SalesPlanController {
	@Autowired
	private SalesPlanServiceImpl spsi;

	@RequestMapping("/listSalesPlan")
	@ResponseBody
	public Map<String, Object> listSalesPlan(int currentPage) {
		System.out.println("===========展示页面===========");
		Map<String, Object> map = spsi.listSalsPlan(currentPage);
		return map;
	}
	// 添加销售计划
	@RequestMapping("/saveSalesPlan")
	@ResponseBody
	public Message saveSalesPlan(HeyunSalesPlan hsp,HttpSession session) {
		System.out.println("==============================");
		System.out.println("销售计划人的名字是=====" + hsp.getName());
		
		// 获取一个当前时间,通过SimpleDateFormat转换为想要格式的String类型的值,存入数据库
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.currentTimeMillis();
		String finalOperTime = sdf.format(System.currentTimeMillis());
		//获取操作人姓名
		HeyunEmp emp=(HeyunEmp) session.getAttribute("user");
		String operator=emp.getEmpRealName();
		hsp.setFinalOperTime(finalOperTime);
		hsp.setOperator(operator);
		int result = spsi.saveSalesPlan(hsp);
		System.out.println("===========" + result);
		return Message.success();
	}

	// 修改销售计划
	@RequestMapping("/updateSalesPlanById")
	@ResponseBody
	public Message updateSalesPlanById(HeyunSalesPlan hsp) {
		System.out.println("===========修改销售计划=============");
		System.out.println("salesplanid==========" + hsp.getSalesPlanId());
		System.out.println("name========" + hsp.getName());
		System.out.println("month========" + hsp.getMonth());
		System.out.println("salesPlanContent========"
				+ hsp.getSalesPlanContent());
		spsi.updateSalesPlanById(hsp);
		return Message.success();
	}
	/*
	 *  单个删除销售计划
	 *  
	*/
	@RequestMapping(value = "/deleteSalesPlanById/{salesPlanId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteSalsPlanById(
			@PathVariable("salesPlanId") Integer salesPlanId) {
		System.err.println("要删除的id是========" + salesPlanId);
		spsi.deleteSalesPlan(salesPlanId);

		return Message.success().add("message", "删除成功啦!!");
	}
	/*
	 *  批量删除删除销售计划
	 *  
	*/
	@RequestMapping(value = "/deleteBatchSalsPlan/{attr}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteBatchSalsPlan(@PathVariable("attr") String attr) {
		System.err.println("要删除的id是========" + attr);
		String[] ids_str= attr.split(",");
		List<Integer> ids = new ArrayList<Integer>();
		for (String string : ids_str) {
			ids.add(Integer.valueOf(string));
		}
		System.out.println("///////////"+ids);
		spsi.deleteBatchSalesPlan(ids);

		return Message.success().add("message", "删除成功啦!!");
	}
	/*
	 * 提交销售计划 
	 */
	@RequestMapping(value = "/updateSalesPlanStateById/{salesPlanId}", method = RequestMethod.PUT)
	@ResponseBody
	public Message updateSalesPlanStateById(@PathVariable("salesPlanId") Integer salesPlanId) {
		System.out.println("salesPlanId======????"+salesPlanId);
		spsi.updateSalesPlanStateById(salesPlanId);
		return Message.success().add("message", "已提交");
	}
	

	/*
	 * 根据销售计划编号查找本条销售计划的内容
	 */
	@RequestMapping(value = "findSalesPlanById/{salesPlanId}", method = RequestMethod.GET)
	@ResponseBody
	public Message findSalesPlanById(
			@PathVariable("salesPlanId") Integer salesPlanId) {
		System.out.println("=========进来了么??++++++++=");
		HeyunSalesPlan hsp = spsi.selectSalesPlanById(salesPlanId);
		return Message.success().add("hsp", hsp);
	}
   
	/*
	 * 根据销售月份查找销售计划
	 */
	@RequestMapping("/findSalesPlanByMonth")
	@ResponseBody
	public Map<String, Object> findSalesPlanByMonth(int currentPage,String month) {
		System.out.println("查询页数"+currentPage);
		System.out.println("要查询的月份是"+month);
		Map<String,Object> map = spsi.findSalesPlanByMonth(currentPage,month);
		return map;
	}

}
